package com.mianyang.zhangsan.service.impl;

import com.mianyang.zhangsan.model.entity.AccountDetail;
import com.mianyang.zhangsan.model.entity.SecAccount;
import com.mianyang.zhangsan.service.ISecAccountService;
import com.mianyang.zhangsan.service.ISecAuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiangjr
 * @date 2022-10-23
 */
@Component
public class SecAccountDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private ISecAccountService accountService;
    @Autowired
    private ISecAuthorityService authorityService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 查询用户信息
        SecAccount account = accountService.selectAccountByUsername(username);
        if (account == null) {
            throw new RuntimeException("用户名或密码错误");
        }
        // 查询用户权限信息
//        List<String> permissions = authorityService.selectAuthorities(account.getAccountId());
        return new AccountDetail(account, new ArrayList<>());
    }
}
